// pages/work-center/work-center.js
var request = require('../../utils/request.js')
var modal = require('../../utils/modal.js')
var utils = require('../../utils/utils.js')
var config = require('../../config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navItems: [{
            name: '工单',
            checked: true
        }, {
            name: '订单',
            checked: false
        }, {
            name: '保险',
            checked: false
        }],
        curItem: '',
        curDate: '',
        curDatepicker: '',
        current_date: '',
        total_amount: '',
        pagination: {},
        list: [],
        reqParams: {
            shop_oid: '',
            p: 1,
            page_size: 10
        },
        reqIsnParams: {
            shop_oid: '',
            month: '',
            p: 1,
            page_size: 10
        },
        reqOrderParams: {
            month: '',
            p: 1,
            page_size: 10
        },
        curr_month: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        var data = new Date()
        var formartDate = data.getFullYear() + '年' + Number(data.getMonth() + 1) + '月'
        this.setData({
            curDate: formartDate,
            curDatepicker: data.getFullYear() + '-' + Number(data.getMonth() + 1)
        })
       
        if (options.type) {
            that.checked2(options.type)
        }else{
          that.checked2(1)
          // if (options.date) {
          //   var navItems = this.data.navItems
          //   navItems.forEach((item, index) => {
          //     if (index == 1) {
          //       item.checked = true
          //     } else {
          //       item.checked = false
          //     }
          //   })

          //   this.setData({
          //     navItems: navItems,
          //     curItem: 1,
          //     curDate: options.fmtDate,
          //     'reqIsnParams.month': options.date
          //   })

          //   this.getOrder()
          // } else {
          //   this.loadData()
          // }
        }

    },
    lower() {
        if (Math.ceil(Number(this.data.pagination.total) / Number(this.data.pagination.page_size)) < ++this.data.pagination.current_page) {
            return;
        }
        switch (this.data.curItem) {
            case 0:
                this.data.reqParams.p++
                this.loadData()
                break;
            case 1:
                this.data.reqOrderParams.p++
                this.getOrder()
                break;
            case 2:
                this.data.reqIsnParams.p++
                this.getInsurance()
                break;
            default:
                break;
        }
    },
    loadData() {
        var that = this
        modal.loading()
        this.data.reqParams.shop_oid = 'GSID8QEW04OO0'
        request.post({path: config.workList}, this.data.reqParams).then(res => {
            modal.hide()

            that.setData({
                current_date: res.data.data.current_date,
                list: that.data.list.concat(res.data.data.list),
                pagination: res.data.data.pagination
            })
        })
    },
    goDetail(e) {
        var id = e.currentTarget.dataset.id
        var typePage = e.currentTarget.dataset.type
        wx.navigateTo({
            url: `../work-detail/work-detail?id=${id}&type=${typePage}`,
        })
    },

    toPolicyDetail(e) {
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `../policy-detail/policy-detail?id=${id}`,
        })
    },
    uploadInvoice(e) {
        wx.navigateTo({
            url: `../upload-invoice/upload-invoice?amount=${e.currentTarget.dataset.invoiceAmount}&number=${e.currentTarget.dataset.invoiceNumber}&id=${e.currentTarget.dataset.id}&fmtDate=${this.data.curDate}&date=${this.data.reqOrderParams.month}`,
        })
    },
    bindDateChange(e) {
        var month = e.detail.value.substring(5, 6) == 0 ? e.detail.value.substring(6) : e.detail.value.substring(5)
        var formartDate = e.detail.value.substring(0, 4) + '年' + month + '月'


        switch (this.data.curItem) {
            case 0:

                break;
            case 1:
                this.setData({
                    curDate: formartDate,
                    'reqOrderParams.month': e.detail.value,
                    list: []
                })
                this.getOrder()
                break;
            case 2:
                this.setData({
                    curDate: formartDate,
                    'reqIsnParams.month': e.detail.value,
                    list: []
                })
                this.getInsurance()
                break;
            default:
                break;
        }

    },
    checked2: function (num) {
        let index = 0;
        if(num){
            index = num -1;
        }
        // 标题
        var navItems = this.data.navItems
        // 取消所有title的选中
        for (var i = 0; i < navItems.length; i++) {
            navItems[i]['checked'] = false
        }
        ;
        // 选中当前title
        navItems[index]['checked'] = true
        this.setData({
            navItems: navItems,
            curItem: index,
            list: [],
            'reqIsnParams.month': utils.formatTime(new Date()).substring(0, 7),
            'reqOrderParams.month': utils.formatTime(new Date()).substring(0, 7),
            'reqOrderParams.p': 1,
            'reqIsnParams.p': 1,
            'reqParams.p': 1
        })
        switch (index) {
            case 0:
                this.loadData()
                break;
            case 1:
                this.getOrder()
                break;
            case 2:
                this.getInsurance()
                break;
            default:
                break;
        }
    },
    checked: function (e) {
        var index = e.target.dataset.index;
        // 标题
        var navItems = this.data.navItems
        // 取消所有title的选中
        for (var i = 0; i < navItems.length; i++) {
            navItems[i]['checked'] = false
        }
        ;
        // 选中当前title
        navItems[index]['checked'] = true
        this.setData({
            navItems: navItems,
            curItem: index,
            list: [],
            'reqIsnParams.month': utils.formatTime(new Date()).substring(0, 7),
            'reqOrderParams.month': utils.formatTime(new Date()).substring(0, 7),
            'reqOrderParams.p': 1,
            'reqIsnParams.p': 1,
            'reqParams.p': 1
        })
        switch (index) {
            case 0:
                this.loadData()
                break;
            case 1:
                this.getOrder()
                break;
            case 2:
                this.getInsurance()
                break;
            default:
                break;
        }
    },
    getOrder() {
        var that = this
        modal.loading()
        // this.data.reqIsnParams.shop_oid = 'A3T2GENDPMC8'
        request.post({path: config.orderList}, this.data.reqOrderParams).then(res => {
            modal.hide()

            that.setData({
                curr_month: res.data.data.curr_month,
                list: that.data.list.concat(res.data.data.list),
                pagination: res.data.data.pagination
            })
        })
    },
    getInsurance() {
        var that = this
        modal.loading()
        this.data.reqIsnParams.shop_oid = 'A3T2GENDPMC8'
        request.post({path: config.insuranceList}, this.data.reqIsnParams).then(res => {
            modal.hide()

            that.setData({
                total_amount: res.data.data.total_amount,
                list: that.data.list.concat(res.data.data.list),
                pagination: res.data.data.pagination
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    // onReachBottom: function () {

    // },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})